<?php
/**
Task:
The sum of all natural numbers below 10 that are multiples of 3 or 5 are 23 (3 + 5 + 6 + 9)

Write a php script that will find the sum of all the multiples of 3 or 5 below 1000. The script 

should run from command line and put the result on screen. We will judge this task based on 

simplicity, efficiency and cleverness of the code.

Extra: Create a second algorithm to find the sum of all the multiples of 3 and 4 below 1000.
*/

function sumMultiples($a, $b, $limit)
{
    $sum = 0;
    $start = $a < $b ? $a : $b;
    for ($i = $start; $i <= $limit; $i ++) {
        if ($i % $a == 0 || $i % $b == 0) {
            $sum += $i;
        }
    }
    return $sum;
}

function validate()
{
    $errorMsg = "";
    $arguments = func_get_args();
    foreach ($arguments as $argument) {
        if (! is_numeric($argument)) {
            $errorMsg .= "ERROR: Invalid argument '" . $argument . "'. \n";
        }
    }
    return $errorMsg;
}

function formatResult($a, $b, $limit)
{
    $msg = "";
    $error = validate($a, $b, $limit);
    if ($error) {
        $msg = $error;
    } else {
        $sum = sumMultiples($a, $b, $limit);
        $msg = "The sum of all multiples of " . $a . " and " . $b . " below " . $limit . " is: " . number_format($sum) . ".\n";
    }
    return $msg;
}

echo formatResult(3, 5, 1000);
echo formatResult(3, 4, 1000);